package pers.jing.android.helper.page

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import pers.jing.android.helper.AndroidSdkTools
import pers.jing.android.helper.model.STATE_FAILURE
import pers.jing.android.helper.model.STATE_NONE
import pers.jing.android.helper.model.STATE_SUCCESS
import pers.jing.android.helper.model.UIProgressDialogState
import pers.jing.android.helper.model.UIDialogState
import pers.jing.android.helper.ANDROID_SDK_PATH
import pers.jing.android.helper.AppUtils
import pers.jing.android.helper.db.Database
import pers.jing.android.helper.model.CODE_SUCCESS

/**
 * 设置页面的ViewModel
 */
class SettingsViewModel(val database: Database): ViewModel() {

    private val _result = MutableStateFlow<UIDialogState>(UIDialogState(0))
    val result = _result.asStateFlow()
    private val _androidSdkPath = MutableStateFlow<String>("")
    val androidSdkPath = _androidSdkPath.asStateFlow()
    private val _progress = MutableStateFlow<UIProgressDialogState>(UIProgressDialogState(false))
    val progress = _progress.asStateFlow()

    fun loadConfig() {
        viewModelScope.launch(Dispatchers.IO) {
            val path = database.getConfig(ANDROID_SDK_PATH)
            if (path != null) {
                _androidSdkPath.update {
                    path
                }
            }
        }
    }

    /**
     * 安装
     * 不需要在当前方法内更新_androidSdkPath
     */
    fun install(androidSdkPath: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val adbVersion = AndroidSdkTools.getAdbVersion(androidSdkPath)
            if (adbVersion.isEmpty()) {
                //开始安装adb
                try {
                    showProgress()
                    AndroidSdkTools.installPlatformTools(androidSdkPath)
                    //保存设置路径
                    database.saveConfig(ANDROID_SDK_PATH, androidSdkPath)
                    hideProgress()
                    _result.update {
                        it.copy(STATE_SUCCESS, "安装成功")
                    }
                } catch (e: Exception) {
                    _result.update {
                        it.copy(STATE_FAILURE, e.message)
                    }
                }
            } else {
                database.saveConfig(ANDROID_SDK_PATH, androidSdkPath)
                _result.update {
                    it.copy(STATE_SUCCESS, "安装成功")
                }
            }
        }
    }

    fun clearResult() {
        _result.update {
            it.copy(state = STATE_NONE)
        }
    }

    fun updateAndroidSdkPath(androidSdkPath: String) {
        _androidSdkPath.update {
            androidSdkPath
        }
    }

    fun showProgress() {
        _progress.update {
            it.copy(true)
        }
    }

    fun hideProgress() {
        _progress.update {
            it.copy(false)
        }
    }

    fun addApkContextMenu() {
        viewModelScope.launch(Dispatchers.IO) {
            val cResult = AppUtils.addApkContextMenu()
            if (CODE_SUCCESS == cResult.code) {
                _result.update {
                    var msg = cResult.result?.joinToString("\n")
                    if (msg.isNullOrEmpty()) {
                        msg = "安装成功"
                    }
                    it.copy(STATE_SUCCESS, msg)
                }
            } else {
                _result.update {
                    it.copy(STATE_FAILURE, cResult.error?.joinToString("\n")?:"unknown")
                }
            }
        }
    }

    fun deleteApkContextMenu() {
        viewModelScope.launch(Dispatchers.IO) {
            var cResult = AppUtils.deleteApkContextMenu()
            if (CODE_SUCCESS == cResult.code) {
                _result.update {
                    var msg = cResult.result?.joinToString("\n")
                    if (msg.isNullOrEmpty()) {
                        msg = "卸载成功"
                    }
                    it.copy(STATE_SUCCESS, msg)
                }
            } else {
                _result.update {
                    it.copy(STATE_FAILURE, cResult.error?.joinToString("\n")?:"unknown")
                }
            }
        }
    }

}